package com.jzo2o.foundations.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * TODO
 *
 * @Author anjing
 * @Date 2024/12/1 13:08
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private IServeItemService serveItemService;
    @Resource
    private IRegionService regionService;
    @Resource
    private IServeItemService itemService;
    @Override
    public List<Long> queryServeItemIdListByCityCode(String cityCode) {
        return null;
    }

    @Override
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        for (ServeUpsertReqDTO dto : serveUpsertReqDTOList) {
            //1.判断服务项是否存在
            ServeItem item = serveItemService.getById(dto.getServeItemId());
            if(ObjectUtil.isEmpty(item)) {
                throw new ForbiddenOperationException("服务项不存在不能添加");
            }

            //2.判断服务项是否未启动
            if(item.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务项未启动不能添加");
            }

            //3.判断服务项是否已添加过
            Integer count = this.lambdaQuery().eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if(count>0) {
                throw new ForbiddenOperationException("服务项不能重复添加");
            }

            //4.查询区域信息
            String cityCode = regionService.getById(dto.getRegionId()).getCityCode();

            //5.复制服务项为服务信息
            Serve serve = BeanUtils.copyBean(dto, Serve.class);
            serve.setCityCode(cityCode);

            //6.新增区域服务信息
            boolean result = this.save(serve);
            if(!result) {
                throw new DBException("添加服务项失败");
            }
        }
    }

    @Override
    public Serve update(Long id, BigDecimal price) {
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("不存在这个服务，禁止修改");
        }
        boolean update = lambdaUpdate().set(Serve::getPrice, price).eq(Serve::getId, id).update();
        if (!update){
            throw new ForbiddenOperationException("修改价格失败");
        }
        return serve;
    }

    @Override
    public void changeHotStatus(Long id, Integer flag) {

    }

    @Override
    public int queryServeCountByRegionIdAndSaleStatus(Long regionId, Integer saleStatus) {
        return 0;
    }

    @Override
    public int queryServeCountByServeItemIdAndSaleStatus(Long serveItemId, Integer saleStatus) {
        return 0;
    }

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {

        ///
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    @Resource
    private ServeMapper serveMapper;

    //删除区域服务
    @Override
    public void deleteById(Long id) {
        //1863207879119441922
        Serve serve = getById(id);
        Long regionId = serve.getRegionId();
        //判断地区
        Region region = regionService.getById(regionId);
        if(ObjectUtil.isEmpty(region)) {
            throw new ForbiddenOperationException("服务没有绑定地区,删除失败");
        }
        //判断服务项
        ServeItem itemServe = itemService.getById(serve.getServeItemId());
        if(BeanUtils.isEmpty(itemServe)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //.判断服务是否存在
        if(ObjectUtil.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在,删除失败");
        }
        //删除区域服务
        serveMapper.deleteById(id);

    }

    @Override
    public List<Serve> queryHotAndOnSaleServeList() {
        return null;
    }

    @Override
    public ServeAggregationSimpleResDTO findDetailById(Long id) {
        return null;
    }

    @Override
    public List<ServeAggregationSimpleResDTO> findHotServeListByRegionId(Long regionId) {
        return null;
    }

    @Override
    public List<ServeAggregationTypeSimpleResDTO> findServeTypeListByRegionId(Long regionId) {
        return null;
    }

    @Override
    public Serve onSale(Long id) {
        Serve serve = getById(id);
        //判断服务是否存在
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，禁止上架");
        }
        //判断地区是否存在
        if (BeanUtils.isEmpty(regionService.getById(serve.getRegionId()))){
            throw new ForbiddenOperationException("不存在地区");
        }
        //判断服务是否上架过
        if (serve.getSaleStatus()==FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("不允许重复上架");
        }
        //判断服务项是否存在
        ServeItem itemServe = itemService.getById(serve.getServeItemId());
        if(BeanUtils.isEmpty(itemServe)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //判断服务项是否启用
        if(itemServe.getActiveStatus() == FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启用");
        }
        //上架
        boolean update = lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus()).update();
        if (!update){
            throw new DBException("上架失败");
        }


        return null;
    }

    @Override
    public Serve offSale(Long id) {

        Serve serve = getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，下架失败");
        }

        ServeItem itemServe = itemService.getById(serve.getServeItemId());
        if(BeanUtils.isEmpty(itemServe)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //判断服务的状态
        if(serve.getSaleStatus()==FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("服务已下架，不能重复下架");
        }
        //修改
        boolean update = lambdaUpdate().eq(Serve::getId, id).set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus()).update();
        if (!update){
            throw new DBException("下架失败");
        }

        return null;
    }

    @Override
    public ServeAggregationResDTO findServeDetailById(Long id) {
        return null;
    }

    @Override
    public boolean saveBatch(Collection<Serve> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Serve> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Serve> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Serve entity) {
        return false;
    }

    @Override
    public Serve getOne(Wrapper<Serve> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Serve> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Serve> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }



    @Override
    public Class<Serve> getEntityClass() {
        return null;
    }

    //设置热门
    @Override
    public void onHot(Long id) {
        Serve serve = getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，设置热门失败");
        }

        //判断地区是否存在
        if (BeanUtils.isEmpty(regionService.getById(serve.getRegionId()))){
            throw new ForbiddenOperationException("不存在地区");
        }
        //判断服务是否热门
        if (serve.getIsHot()==FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("已经设置热门");
        }
        //判断服务项是否存在
        ServeItem itemServe = itemService.getById(serve.getServeItemId());
        if(BeanUtils.isEmpty(itemServe)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        boolean update = lambdaUpdate().eq(Serve::getId, id).set(Serve::getIsHot, FoundationStatusEnum.DISABLE.getStatus()).update();

        if (!update){
            throw new DBException("设置热门失败");
        }

    }

    @Override
    public void offHot(Long id) {
        Serve serve = getById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在，设置热门失败");
        }

        //判断地区是否存在
        if (BeanUtils.isEmpty(regionService.getById(serve.getRegionId()))){
            throw new ForbiddenOperationException("不存在地区");
        }
        //判断服务是否热门
        if (serve.getIsHot()==FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("已经是不热门");
        }
        //判断服务项是否存在
        ServeItem itemServe = itemService.getById(serve.getServeItemId());
        if(BeanUtils.isEmpty(itemServe)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        boolean update = lambdaUpdate().eq(Serve::getId, id).set(Serve::getIsHot, FoundationStatusEnum.INIT.getStatus()).update();

        if (!update){
            throw new DBException("取消热门失败");
        }
    }
}
